//
//  CommentDB.m
//  DialectShare
//
//  Created by timmy zhong on 4/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CommentDB.h"
#import "FMDatabase.h"
#import "PathHelper.h"
#import "Const.h"
#import "Comment.h"

@implementation CommentDB


- (id)init
{
    self = [super init];
    if (self) {
        FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
        if ([db open]) {
            NSString* createTableStatement = [NSString stringWithFormat:
                                              @"""CREATE TABLE IF NOT EXISTS %@"""
                                              """(%@ INTEGER PRIMARY KEY, %@ INTEGER, %@ TEXT, %@ INTEGER, %@ INTEGER)""", 
                                              CommentTableName, CommentId, CommentDiaelctId, CommentContent, CommentDateline, CommentUid];
            BOOL su = [db executeUpdate:createTableStatement]; 
            if (!su) {
                NSLog(@"fail to create table  %@  error  %@", CommentTableName, [db lastErrorMessage]);
            }
            [db close];
        }
    }
    
    return self;
}

- (void)addComments:(NSArray *)comments
{
    FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
    if (![db open]) {
        NSLog(@"addComments: fail to open DB error  %@", [db lastErrorMessage]);
        return;
    }
    
    [db beginTransaction];
    for (Comment *comment in comments) {
        
        NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@, %@, %@, %@, %@) values (?, ?, ?, ?, ?)", CommentTableName , CommentId, CommentDiaelctId, CommentContent, CommentDateline, CommentUid];
        [db executeUpdate:sql,
         [NSNumber numberWithInt:comment.commentId],
         [NSNumber numberWithInt:comment.dialectId],
         comment.content,
         [NSNumber numberWithInt:comment.dateline],
         [NSNumber numberWithInt:comment.uid]];
    }
    
    BOOL su = [db commit];
    if (!su) {
        NSLog(@"fail to add comments error %@", [db lastErrorMessage]);
    }
    [db close];

}

- (NSArray *)commentsOfDialect:(Dialect *)dialect
{
    NSString *condition = [NSString stringWithFormat:@"%@=%d", CommentDiaelctId, dialect.dialectId];
    return [self condition:condition start:0 count:1000];
}

- (NSArray *)condition:(NSString *)condition start:(NSInteger)start count:(NSInteger)count
{
    FMDatabase *db = [FMDatabase databaseWithPath:[PathHelper dbPath]];
    if (![db open]) {
        NSLog(@"get comments: fail to open DB error  %@", [db lastErrorMessage]);
        return nil;
    }
    
    NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ order by %@ desc limit %d, %d", CommentTableName, condition, CommentDateline, start, count];
    FMResultSet *rs = [db executeQuery:sql];
    NSMutableArray *comments = [[NSMutableArray alloc] initWithCapacity:20];
    while ([rs next]) {
        Comment *comment = [[Comment alloc] init];
        comment.commentId = [rs intForColumn:CommentId];
        comment.dialectId = [rs intForColumn:CommentDiaelctId];
        comment.content = [rs stringForColumn:CommentContent];
        comment.dateline = [rs intForColumn:CommentDateline];
        comment.uid = [rs intForColumn:CommentUid];
        [comments addObject:comment];
        [comment release];
    }
    
    [db close];
    
    return [comments autorelease];
    
}


@end
